package com.aegean.MonitorExamSYS.android.dao.impl;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.aegean.MonitorExamSYS.android.component.ExamineeExam;
import com.aegean.MonitorExamSYS.android.dao.AndroidDao;
import com.aegean.MonitorExamSYS.center.dao.CenterDao;
import com.aegean.MonitorExaxSYS.model.BigType;
import com.aegean.MonitorExaxSYS.model.CenterValue;
import com.aegean.MonitorExaxSYS.model.ConductionValue;
import com.aegean.MonitorExaxSYS.model.ExamInfo;
import com.aegean.MonitorExaxSYS.model.ExamineeQualification;
import com.aegean.MonitorExaxSYS.model.ExamineeValue;
import com.aegean.MonitorExaxSYS.model.MonitorValue;
import com.aegean.MonitorExaxSYS.model.SmallType;
import com.aegean.MonitorExaxSYS.model.UserValue;
import com.aegean.MonitorExaxSYS.model.component.Photo;

@Repository
@Transactional
public class AndroidDaoImpl implements AndroidDao {
	@Resource
	SessionFactory factory;

	@Override
	public Object loginUserForAndroid(int status, String name, String pwd,
			int unit_Id) throws Exception {
		// TODO Auto-generated method stub
		Session session;
		Class cl;
		try {
			session = factory.getCurrentSession();
			if (status == 1) {
				cl = ExamineeValue.class;
			} else {
				cl = MonitorValue.class;
			}
			DetachedCriteria dc = DetachedCriteria.forClass(cl);
			dc.add(Restrictions.eq("user_Login", name));
			dc.add(Restrictions.eq("user_Password", pwd));
			if (status == 1) {
			} else {
				ConductionValue con = (ConductionValue) session.load(
						ConductionValue.class, unit_Id);
				dc.add(Restrictions.eq("monitor_Unit", con));
			}
			Criteria cr = dc.getExecutableCriteria(session);
			return cr.uniqueResult();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public UserValue showPhoto(int user_Id) throws Exception {
		// TODO Auto-generated method stub
		Session session;
		UserValue user;
		try {
			session = factory.getCurrentSession();
			user = (UserValue) session.get(UserValue.class, user_Id);
			return user;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public boolean updatePhoto(int user_Id, File photo, String photoFileName,
			String photoContentType) throws Exception {
		// TODO Auto-generated method stub
		Session session;
		UserValue user;
		try {
			session = factory.getCurrentSession();
			user = (UserValue) session.load(UserValue.class, user_Id);
			Photo p = new Photo();
			p.setPhoto(Hibernate.createBlob(new FileInputStream(photo)));
			p.setPhoto_Name(photoFileName);
			p.setPhoto_Type(photoContentType);
			user.setUser_Photo(p);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public List<ExamineeExam> showExamList(int user_Id, int start, int limit) {
		// TODO Auto-generated method stub
		Session session;
		List<ExamineeExam> exs = new ArrayList<ExamineeExam>();
		try {
			session = factory.getCurrentSession();
			String hql = "select exam.examInfo_Id,exam.examInfo_Name,exam.examInfo_StartTime,exam.examInfo_EndTime,eq.examinee_Id,eq.examinee_Identity,eq.examinee_State,con.conduction_Id,con.conduction_Name,ep.examPosition_Id,ep.examPosition_Name,ep.examPosition_Place.place_H,ep.examPosition_Place.place_V,exam.examInfo_Photo.photo_Name,exam.examInfo_Type.small_Id from ExamineeQualification as eq,ExamineeValue as e,ExamPosition as ep,PositionQualification as pq,ExamPositionQualification as epq,ExamInfo as exam,ConductionValue as con"
					+ " where eq.examinee_User=e and e.user_Id=:user_Id and eq.examinee_PQ=epq and ((eq.examineePQ=pq and pq.pq_Ep=ep)or(eq.examineePQ=null)) and epq.examPQ_Exam=exam and exam.examInfo_State in (0,3) and epq.examPQ_Conduction=con"
					+ " group by exam.examInfo_Id order by exam.examInfo_StartTime desc";
			Query q = session.createQuery(hql);
			q.setInteger("user_Id", user_Id);
			q.setCacheable(true);
			q.setFirstResult(start);
			q.setMaxResults(limit);
			List l = q.list();
			for (int i = 0; i < l.size(); i++) {
				Object[] obj = (Object[]) l.get(i);
				ExamineeExam one = new ExamineeExam();
				one.setExam_Id(Integer.parseInt(obj[0].toString().trim()));
				one.setExma_Name(obj[1].toString().trim());
				one.setExam_startTime(obj[2].toString().trim());
				one.setExam_endTime(obj[3].toString().trim());
				one.setExamineeQ_Id(Integer.parseInt(obj[4].toString().trim()));
				if (obj[5] == null) {
					one.setExamineeQ_Identity("无");
				} else {
					one.setExamineeQ_Identity(obj[5].toString().trim());
				}
				one.setExamineeQ_State(Integer.parseInt(obj[6].toString()
						.trim()));
				one.setPlace_Id(Integer.parseInt(obj[7].toString().trim()));
				one.setPlace_Name(obj[8].toString().trim());
				one.setPosition_Id(Integer.parseInt(obj[9].toString().trim()));
				one.setPosition_Name(obj[10].toString().trim());
				one.setP_H(Float.parseFloat((obj[11].toString().trim())));
				one.setP_V(Float.parseFloat((obj[12].toString().trim())));
				one.setExam_PhotoName(obj[13].toString().trim());
				one.setSmall_Id(Integer.parseInt(obj[14].toString().trim()));
				exs.add(one);
			}
			return exs;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public int showExamListCount(int user_Id) {
		// TODO Auto-generated method stub
		Session session;
		try {
			session = factory.getCurrentSession();
			String hql = "select exam.examInfo_Id,exam.examInfo_Name,exam.examInfo_StartTime,exam.examInfo_EndTime,eq.examinee_Id,eq.examinee_Identity,eq.examinee_State,con.conduction_Id,con.conduction_Name,ep.examPosition_Id,ep.examPosition_Name,ep.examPosition_Place.place_H,ep.examPosition_Place.place_V from ExamineeQualification as eq,ExamineeValue as e,ExamPosition as ep,PositionQualification as pq,ExamPositionQualification as epq,ExamInfo as exam,ConductionValue as con"
				+ " where eq.examinee_User=e and e.user_Id=:user_Id and eq.examinee_PQ=epq and ((eq.examineePQ=pq and pq.pq_Ep=ep)or(eq.examineePQ=null)) and epq.examPQ_Exam=exam and exam.examInfo_State in (0,3) and epq.examPQ_Conduction=con"
				+ " group by exam.examInfo_Id order by exam.examInfo_StartTime desc";
			Query q = session.createQuery(hql);
			q.setInteger("user_Id", user_Id);
			q.setCacheable(true);
			return q.list().size();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public boolean editorExamineeStatus(int user_Id, String comments,int status) {
		// TODO Auto-generated method stub
		Session session;
		ExamineeQualification one;
		try {
			session = factory.getCurrentSession();
			one = (ExamineeQualification) session.load(ExamineeQualification.class, user_Id);
			one.setExaminee_State(status);
			one.setExaminee_Comments(comments);
			return true;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public Map<String, String> showExamineeStatus(int user_id) {
		// TODO Auto-generated method stub
		Session session;
		ExamineeQualification one;
		Map<String,String> status=new HashMap<String, String>();
		try {
			session = factory.getCurrentSession();
			one = (ExamineeQualification) session.load(ExamineeQualification.class, user_id);
			status.put("status",String.valueOf(one.getExaminee_State()));
			status.put("comments",one.getExaminee_Comments());
			return status;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return null;
		}
	}

}
